package com.woniuxy.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.user.entity.Perms;
import com.woniuxy.user.mapper.PermsMapper;
import com.woniuxy.user.service.PermsService;
import com.woniuxy.user.vo.AddPermsVo;
import com.woniuxy.user.vo.PermsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Mr·Xiang
 * @create 2024-04-19 11:33
 */
@Service
public class PermsServiceImpl implements PermsService {
    @Resource
    private PermsMapper permsMapper;

    @Override
    public List<String> findByUid(int uid) {
        return permsMapper.findByUid(uid);
    }

    @Override
    public ResPage<Perms> getPermsByRid(PermsVo permsVo) {
        PageHelper.startPage(permsVo.getPage(), permsVo.getSize());
        List<Perms> list = permsMapper.getPermsByRid(permsVo);
        PageInfo<Perms> pageInfo = new PageInfo<>(list);
        return new ResPage<>(pageInfo);
    }

    @Transactional
    @Override
    public Boolean addPerms(AddPermsVo addPermsVo) {
        //添加到权限表
        permsMapper.addPerms(addPermsVo);
        //获取pid,添加到角色权限表
        return permsMapper.addRolePerms(addPermsVo) == 1;
    }

    @Override
    public Boolean editPerms(Perms perms) {
        return permsMapper.editPerms(perms) == 1;
    }

    @Transactional
    @Override
    public Boolean deletePerms(Integer id) {
        permsMapper.deleteRolePerms(id);
        return permsMapper.deletePerms(id) == 1;
    }
}
